package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.common.util.JSONResult;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import javafx.animation.ScaleTransition;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.swing.*;
import java.util.List;

@Controller
public class IndexController {
    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private ISearchService searchService;


    @RequestMapping("")
    public String indes(Model model){
        //推荐的5片游记
        model.addAttribute("tcs",travelCommendService.listTop5());
        //推荐的5片攻略
        model.addAttribute("scs",strategyCommendService.listTop5());
        return "index/index";
    }
    @RequestMapping("index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo")TravelQueryObject qo){
        //推荐的5片游记
        model.addAttribute("pageInfo",travelService.queryForList(qo));
        return "index/travelPageTpl";
    }
        @RequestMapping("q")
    public  String q(@ModelAttribute("qo") SearchQueryObject qo , Model model){
           String viewName=null;
            switch(qo.getType()) {
                case SearchQueryObject.SEARCH_DEST:
                    //目的地查询
                    viewName =viewName="/searchDest";
                            searchDest(qo.getKeyword(),model);
                    break;
                case SearchQueryObject.SEARCH_STRATEGY:
                    //
                    searchStratgy(qo,model);
                    viewName="/searchStrategy";
                    break;
                case SearchQueryObject.SEARCH_TRAVEL:
                    searchTravel(qo,model);
                    viewName="/searchTravel";
                    break;
                case SearchQueryObject.SEARCH_USER:
                    searchUser(qo,model);
                    viewName="/searchUser";
                    break;
                default:
                    searchAll(qo,model);
                    viewName="/searchAll";
            }
        return "index"+viewName;
    }

    private void searchAll(SearchQueryObject qo,Model model) {
        Page<StrategyTemplate> sts = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME, StrategyTemplate.class, qo,
                "title", "subTitle", "summary");
        Page<TravelTemplate> tts = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME, TravelTemplate.class, qo,
                "title", "summary");
        Page<UserInfoTemplate> users = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME, UserInfoTemplate.class, qo,
                "nickname", "destName");

        SearchResult data = new SearchResult();
        data.setTravels(tts.getContent());
        data.setUsers(users.getContent());
        data.setStrategys(sts.getContent());
        data.setTotal(users.getSize()+tts.getSize()+sts.getSize());
        model.addAttribute("data",data);
    }

    private void searchDest(String keyword, Model model) {
        SearchResult data = new SearchResult();

        List<StrategyTemplate> strategys=strategySearchService.listByDestName(keyword);
       List<TravelTemplate> travels=travelSearchService.listByDestName(keyword);
       List<UserInfoTemplate> userInfos=userInfoSearchService.listByDestName(keyword);
        //总数量

        //扩展点:有可能查询到的数据多,应当适当限制
        data.setStrategys(strategys);
        data.setTravels(travels);
        data.setUsers(userInfos);
        DestinationTemplate dest=destinationSearchService.getByName(keyword);
       //data{}
        data.setTotal(strategys.size()+travels.size()+userInfos.size()+ (dest !=null ?1:0));
        model.addAttribute("data",data);
        //dest
        model.addAttribute("dest",dest);
    }



    //攻略
    private void searchStratgy(SearchQueryObject qo, Model model) {
        Page<StrategyTemplate> page = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME, StrategyTemplate.class, qo,
                "title", "subTitle", "summary");
        model.addAttribute("page",page);
    }
    //游记
    private void searchTravel(SearchQueryObject qo, Model model) {
        Page<TravelTemplate> page = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME, TravelTemplate.class, qo,
                "title", "summary");
        model.addAttribute("page",page);
    }
    //用户
    private void searchUser(SearchQueryObject qo, Model model) {
        Page<UserInfoTemplate> page = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME, UserInfoTemplate.class, qo,
                "nickname", "destName");
        model.addAttribute("page",page);
    }
}
